package com.zenithmind.version.adapter;

import com.zenithmind.version.pojo.domain.Repository;
import com.zenithmind.version.pojo.domain.RepositoryConfig;
import com.zenithmind.version.pojo.domain.RepositoryStatistics;
import com.zenithmind.version.pojo.domain.RepositoryUrl;
import com.zenithmind.version.pojo.dto.RepositoryCreateDTO;
import com.zenithmind.version.pojo.dto.RepositoryBasicInfoDTO;
import com.zenithmind.version.pojo.dto.RepositoryConfigDTO;
import com.zenithmind.version.pojo.dto.RepositoryInitDTO;
import com.zenithmind.version.service.RepositoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 仓库服务适配器 - 遵循适配器模式，提供向后兼容性
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
// @Component  // 临时禁用，解决启动问题
@RequiredArgsConstructor
public class RepositoryServiceAdapter {

    private final RepositoryService repositoryService;

    /**
     * 适配旧版本的RepositoryCreateDTO
     * 
     * @param legacyDTO 旧版本DTO
     * @return 新版本DTO
     */
    public RepositoryCreateDTO adaptLegacyCreateDTO(LegacyRepositoryCreateDTO legacyDTO) {
        RepositoryCreateDTO newDTO = new RepositoryCreateDTO();
        newDTO.setOwnerId(legacyDTO.getOwnerId());
        newDTO.setTags(legacyDTO.getTags());

        // 适配基本信息
        RepositoryBasicInfoDTO basicInfo = new RepositoryBasicInfoDTO();
        basicInfo.setName(legacyDTO.getName());
        basicInfo.setDescription(legacyDTO.getDescription());
        basicInfo.setType(legacyDTO.getType());
        basicInfo.setDefaultBranch(legacyDTO.getDefaultBranch());
        basicInfo.setLanguage(legacyDTO.getLanguage());
        newDTO.setBasicInfo(basicInfo);

        // 适配配置信息
        RepositoryConfigDTO config = new RepositoryConfigDTO();
        config.setAllowFork(legacyDTO.getAllowFork());
        config.setAllowIssues(legacyDTO.getAllowIssues());
        config.setAllowWiki(legacyDTO.getAllowWiki());
        config.setHomepage(legacyDTO.getHomepage());
        newDTO.setConfig(config);

        // 适配初始化信息
        RepositoryInitDTO initInfo = new RepositoryInitDTO();
        initInfo.setInitReadme(legacyDTO.getInitReadme());
        initInfo.setInitGitignore(legacyDTO.getInitGitignore());
        initInfo.setGitignoreTemplate(legacyDTO.getGitignoreTemplate());
        initInfo.setInitLicense(legacyDTO.getInitLicense());
        initInfo.setLicenseTemplate(legacyDTO.getLicenseTemplate());
        newDTO.setInitInfo(initInfo);

        return newDTO;
    }

    /**
     * 适配旧版本的Repository查询结果
     * 
     * @param repository 仓库基本信息
     * @param statistics 统计信息
     * @param config 配置信息
     * @param url URL信息
     * @return 旧版本格式的Repository
     */
    public LegacyRepository adaptToLegacyRepository(Repository repository, 
                                                   RepositoryStatistics statistics,
                                                   RepositoryConfig config,
                                                   RepositoryUrl url) {
        LegacyRepository legacy = new LegacyRepository();
        
        // 复制基本信息
        legacy.setId(repository.getId());
        legacy.setName(repository.getName());
        legacy.setDescription(repository.getDescription());
        legacy.setOwnerId(repository.getOwnerId());
        legacy.setOwnerName(repository.getOwnerName());
        legacy.setType(repository.getType());
        legacy.setStatus(repository.getStatus());
        legacy.setDefaultBranch(repository.getDefaultBranch());
        legacy.setLanguage(repository.getLanguage());
        legacy.setTags(repository.getTags());
        legacy.setLastPushTime(repository.getLastPushTime());
        legacy.setLastActivityTime(repository.getLastActivityTime());
        legacy.setSortOrder(repository.getSortOrder());

        // 复制统计信息
        if (statistics != null) {
            legacy.setSize(statistics.getSize());
            legacy.setFileCount(statistics.getFileCount());
            legacy.setCommitCount(statistics.getCommitCount());
            legacy.setBranchCount(statistics.getBranchCount());
            legacy.setTagCount(statistics.getTagCount());
            legacy.setContributorCount(statistics.getContributorCount());
        }

        // 复制配置信息
        if (config != null) {
            legacy.setAllowFork(config.getAllowFork());
            legacy.setAllowIssues(config.getAllowIssues());
            legacy.setAllowWiki(config.getAllowWiki());
            legacy.setHomepage(config.getHomepage());
        }

        // 复制URL信息
        if (url != null) {
            legacy.setCloneUrl(url.getCloneUrl());
            legacy.setSshUrl(url.getSshUrl());
        }

        return legacy;
    }

    /**
     * 旧版本的RepositoryCreateDTO（用于兼容性）
     */
    public static class LegacyRepositoryCreateDTO {
        private String ownerId;
        private String name;
        private String description;
        private Integer type;
        private String defaultBranch;
        private String language;
        private String tags;
        private String homepage;
        private Integer allowFork;
        private Integer allowIssues;
        private Integer allowWiki;
        private Integer initReadme;
        private Integer initGitignore;
        private String gitignoreTemplate;
        private Integer initLicense;
        private String licenseTemplate;

        // Getters and Setters
        public String getOwnerId() { return ownerId; }
        public void setOwnerId(String ownerId) { this.ownerId = ownerId; }
        
        public String getName() { return name; }
        public void setName(String name) { this.name = name; }
        
        public String getDescription() { return description; }
        public void setDescription(String description) { this.description = description; }
        
        public Integer getType() { return type; }
        public void setType(Integer type) { this.type = type; }
        
        public String getDefaultBranch() { return defaultBranch; }
        public void setDefaultBranch(String defaultBranch) { this.defaultBranch = defaultBranch; }
        
        public String getLanguage() { return language; }
        public void setLanguage(String language) { this.language = language; }
        
        public String getTags() { return tags; }
        public void setTags(String tags) { this.tags = tags; }
        
        public String getHomepage() { return homepage; }
        public void setHomepage(String homepage) { this.homepage = homepage; }
        
        public Integer getAllowFork() { return allowFork; }
        public void setAllowFork(Integer allowFork) { this.allowFork = allowFork; }
        
        public Integer getAllowIssues() { return allowIssues; }
        public void setAllowIssues(Integer allowIssues) { this.allowIssues = allowIssues; }
        
        public Integer getAllowWiki() { return allowWiki; }
        public void setAllowWiki(Integer allowWiki) { this.allowWiki = allowWiki; }
        
        public Integer getInitReadme() { return initReadme; }
        public void setInitReadme(Integer initReadme) { this.initReadme = initReadme; }
        
        public Integer getInitGitignore() { return initGitignore; }
        public void setInitGitignore(Integer initGitignore) { this.initGitignore = initGitignore; }
        
        public String getGitignoreTemplate() { return gitignoreTemplate; }
        public void setGitignoreTemplate(String gitignoreTemplate) { this.gitignoreTemplate = gitignoreTemplate; }
        
        public Integer getInitLicense() { return initLicense; }
        public void setInitLicense(Integer initLicense) { this.initLicense = initLicense; }
        
        public String getLicenseTemplate() { return licenseTemplate; }
        public void setLicenseTemplate(String licenseTemplate) { this.licenseTemplate = licenseTemplate; }
    }

    /**
     * 旧版本的Repository（用于兼容性）
     */
    public static class LegacyRepository extends Repository {
        private Long size;
        private Integer fileCount;
        private Integer commitCount;
        private Integer branchCount;
        private Integer tagCount;
        private Integer contributorCount;
        private Integer allowFork;
        private Integer allowIssues;
        private Integer allowWiki;
        private String homepage;
        private String cloneUrl;
        private String sshUrl;

        // Getters and Setters for legacy fields
        public Long getSize() { return size; }
        public void setSize(Long size) { this.size = size; }
        
        public Integer getFileCount() { return fileCount; }
        public void setFileCount(Integer fileCount) { this.fileCount = fileCount; }
        
        public Integer getCommitCount() { return commitCount; }
        public void setCommitCount(Integer commitCount) { this.commitCount = commitCount; }
        
        public Integer getBranchCount() { return branchCount; }
        public void setBranchCount(Integer branchCount) { this.branchCount = branchCount; }
        
        public Integer getTagCount() { return tagCount; }
        public void setTagCount(Integer tagCount) { this.tagCount = tagCount; }
        
        public Integer getContributorCount() { return contributorCount; }
        public void setContributorCount(Integer contributorCount) { this.contributorCount = contributorCount; }
        
        public Integer getAllowFork() { return allowFork; }
        public void setAllowFork(Integer allowFork) { this.allowFork = allowFork; }
        
        public Integer getAllowIssues() { return allowIssues; }
        public void setAllowIssues(Integer allowIssues) { this.allowIssues = allowIssues; }
        
        public Integer getAllowWiki() { return allowWiki; }
        public void setAllowWiki(Integer allowWiki) { this.allowWiki = allowWiki; }
        
        public String getHomepage() { return homepage; }
        public void setHomepage(String homepage) { this.homepage = homepage; }
        
        public String getCloneUrl() { return cloneUrl; }
        public void setCloneUrl(String cloneUrl) { this.cloneUrl = cloneUrl; }
        
        public String getSshUrl() { return sshUrl; }
        public void setSshUrl(String sshUrl) { this.sshUrl = sshUrl; }
    }
}
